﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_link_Default : System.Web.UI.Page
{
    static Cstudy.MODEL.link link;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            link = new Cstudy.MODEL.link();
            Boundlink();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadLink();
        //Response.Write(GridView1.SelectedRow.Cells[0].Text);
    }

    protected void Boundlink()
    {
        DataSet ds = new DataSet();
        ds = new Cstudy.BLL.link().SelectLink();
        if (ds != null)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }

    protected void LoadLink()
    {
        link.link_id = int.Parse(GridView1.SelectedRow.Cells[0].Text);
        link.link_name = GridView1.SelectedRow.Cells[1].Text;
        link.link_url = GridView1.SelectedRow.Cells[2].Text;
        link.link_remark = GridView1.SelectedRow.Cells[3].Text;
        link.link_order = int.Parse(GridView1.SelectedRow.Cells[4].Text);
        NameBox.Text = link.link_name;
        UrlBox.Text = link.link_url;
        RemarkBox.Text = link.link_remark;
        OrderBox.Text = link.link_order.ToString();
    }
    protected void AddBtn_Click(object sender, EventArgs e)
    {
        if (CanAdd())
        {
            link.link_name = NameBox.Text;
            link.link_remark = RemarkBox.Text;
            link.link_url = UrlBox.Text;
            link.link_order = int.Parse(OrderBox.Text);
            if (new Cstudy.BLL.link().AddLink(link))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "success", "alert('添加成功！')", true);
                Boundlink();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "error", "alert('添加失败！')", true);
            }
        }
    }

    protected bool CanAdd()
    {
        int o;
        if (NameBox.Text.Trim().Length < 1)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "error", "alert('链接名不能为空！')", true);
            return false;
        }
        if (!UrlBox.Text.ToLower().StartsWith("http://"))
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "error", "alert('链接地址不正确！')", true);
            return false;
        }
        if (!int.TryParse(OrderBox.Text,out o))
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "error", "alert('链接顺序必须为数字！')", true);
            return false;
        }
        return true;
    }
    protected void EditBtn_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex == -1)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "success", "alert('请先选择一项修改！')", true);
            return;
        }
        if (CanAdd())
        {
            link.link_name = NameBox.Text;
            link.link_remark = RemarkBox.Text;
            link.link_url = UrlBox.Text;
            link.link_order = int.Parse(OrderBox.Text);
            if (new Cstudy.BLL.link().UpdateLink(link))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "success", "alert('修改成功！')", true);
                Boundlink();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "error", "alert('修改失败！')", true);
            }
        }
    }
    protected void DelBtn_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex == -1)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "success", "alert('请先选择一项删除！')", true);
            return;
        }
        if (new Cstudy.BLL.link().DeleteLink(link.link_id))
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "success", "alert('删除成功！')", true);
            Boundlink();
        }
        else
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "error", "alert('删除失败！')", true);
        }
    }
}